package com.ayoka.ui.course.course_estimate;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.databinding.ActivityCourseEstimateQuestionBinding;
import com.ayoka.mvp.CourseEstimateMVP;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimateModel.CourseEstimateDescriptionBean;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimateModel.ResponseCourseEstimateBean;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimateModel.CourseEstimateReportBean;
import com.ayoka.mvp.CourseEstimateMVP.CourseEstimatePresenter;
import com.ayoka.mvp.CourseEstimateMVP.ICourseEstimateView;
import com.ayoka.utils.CountDownEvent;
import com.jcp.baselibrary.base.APPActivityManager;
import com.jcp.baselibrary.dialog.DialogHelper;
import com.jcp.baselibrary.model.ToolBarOptions;
import com.jcp.baselibrary.utils.DateUtil;

import java.util.Observable;


/**
 * =========================================================
 * @ProjectName:
 * @ClassName: CourseEstimateQuestionActivity
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/28 10:48
 * =========================================================
 */
public class CourseEstimateQuestionActivity extends BaseActivity<ActivityCourseEstimateQuestionBinding, CourseEstimatePresenter> implements ICourseEstimateView{

    public static void start(Activity activity, long courseEstimateId) {
        Intent intent = new Intent(activity, CourseEstimateQuestionActivity.class);
        intent.putExtra("CourseEstimateId", courseEstimateId);
        activity.startActivity(intent);
    }

    private long courseEstimateId;

    private ResponseCourseEstimateBean responseCourseEstimateBean;

    private int currentIndex;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null){
            courseEstimateId = intent.getLongExtra("CourseEstimateId", 0);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_course_estimate_question;
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(R.string.little_estimate);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        mPresenter.getCourseEstimateDetailData(courseEstimateId);
    }

    @Override
    protected CourseEstimatePresenter getPresenter() {
        return new CourseEstimatePresenter();
    }


    @Override
    public void configCourseEstimateDescriptionData(CourseEstimateDescriptionBean courseEstimateDescriptionBean) {

    }

    @Override
    public void configCourseEstimateDetailData(ResponseCourseEstimateBean responseCourseEstimateBean) {
        if (responseCourseEstimateBean == null || responseCourseEstimateBean.testing_info == null) return;
        this.responseCourseEstimateBean = responseCourseEstimateBean;
        // 倒计时开启
        CountDownEvent.getInstance().start(responseCourseEstimateBean.testing_info.LimitTime * 60 * 1000);
        CountDownEvent.getInstance().addObserver(this);
        // 设置顶部进度条
        configQuestionProgress();
        // 设置问题Fragment
        configQuestionFragment();
    }

    @Override
    public void onSubmitCourseEstimateCallback(boolean isSuccess, int recordId) {

    }

    @Override
    public void configCourseEstimateReportData(CourseEstimateReportBean courseEstimateReportBean) {

    }

    private void configQuestionProgress() {
        mDataBinding.tvCurrent.setText(String.valueOf(currentIndex + 1));
        mDataBinding.tvAll.setText(String.valueOf("/" + responseCourseEstimateBean.testing_info.QuestionCount));
        mDataBinding.pbQuestion.setMax(responseCourseEstimateBean.testing_info.QuestionCount);
        mDataBinding.pbQuestion.setProgress(currentIndex + 1);
    }

    private void configQuestionFragment() {
        if (responseCourseEstimateBean.testing_info.Questions == null || responseCourseEstimateBean.testing_info.Questions.isEmpty()) return;
        CourseEstimateMVP.CourseEstimateModel.CourseEstimateQuestionBean courseEstimateQuestionBean = responseCourseEstimateBean.testing_info.Questions.get(currentIndex);
        CourseEstimateQuestionFragment questionFragment = CourseEstimateQuestionFragment.newInstance(currentIndex == 0, courseEstimateQuestionBean, responseCourseEstimateBean.record_id);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setCustomAnimations(R.anim.slide_right_in,
                R.anim.slide_left_out,
                R.anim.slide_left_in,
                R.anim.slide_right_out);
        Fragment fragmentById = fragmentManager.findFragmentById(R.id.frameLayout);
        if (fragmentById != null) {
            transaction.hide(fragmentById);
        }
        transaction.add(R.id.frameLayout, questionFragment, questionFragment.getClass().getSimpleName())
                .addToBackStack(null)
                .commitAllowingStateLoss();
    }

    public void showNextQuestion() {
        currentIndex++;
        if (currentIndex < responseCourseEstimateBean.testing_info.QuestionCount) {
            configQuestionProgress();
            configQuestionFragment();
        } else {
            CourseEstimateReportActivity.start(this, responseCourseEstimateBean.record_id);
            finish();
        }
    }

    public void showLastQuestion() {
        currentIndex--;
        configQuestionProgress();
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager.getBackStackEntryCount() > 1) {
            fragmentManager.popBackStack();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CountDownEvent.getInstance().deleteObserver(this);
        CountDownEvent.getInstance().cancel();
    }

    @Override
    public void onBackPressed() {
        if (currentIndex > 0) {
            showLastQuestion();
        } else {
            finish();
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        if (observable instanceof CountDownEvent){
            long leftTime = (long) data;
            if (leftTime > 0) {
                mDataBinding.tvAllTime.setText(DateUtil.formatTime(leftTime / 1000));
            } else {
                mDataBinding.tvAllTime.setText("已超时");
                showTimeUpNoticeDialog();
            }
        }
    }

    private void showTimeUpNoticeDialog() {
        DialogHelper.getOkDialog(this, "提示", "已超过规定答题时间，请重新测试。", false, "重新测试", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                finish();
            }
        }).show();
    }
}
